﻿Imports Microsoft.Win32
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Drawing.Drawing2D
Imports System.Globalization
Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.ComponentModel.Design
    Public NotInheritable Class ThemeConfigurationDialog
        Inherits Form
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider)
            Me.New(serviceProvider, Nothing)
        End Sub

        Public Sub New(ByVal serviceProvider As IServiceProvider, ByVal theme As WorkflowTheme)
            If (serviceProvider Is Nothing) Then
                Throw New ArgumentNullException("serviceProvider")
            End If
            Me.serviceProvider = serviceProvider
            If (theme Is Nothing) Then
                Me.bufferedTheme = New WorkflowTheme
                Me.themeDirty = True
            Else
                Me.bufferedTheme = theme
                Me.themeDirty = False
            End If
            Me.bufferedTheme.ReadOnly = False
            Me.InitializeComponent
            Me.themeLocationButton.AutoSize = True
            Me.Font = Me.StandardFont
            AddHandler SystemEvents.UserPreferenceChanged, New UserPreferenceChangedEventHandler(AddressOf Me.OnOperatingSystemSettingsChanged)
        End Sub

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If (Not Me.components Is Nothing) Then
                    Me.components.Dispose
                End If
                RemoveHandler SystemEvents.UserPreferenceChanged, New UserPreferenceChangedEventHandler(AddressOf Me.OnOperatingSystemSettingsChanged)
                If (Not Me.designerPreview Is Nothing) Then
                    Me.designerPreview.Dispose
                    Me.designerPreview = Nothing
                End If
                If (Not Me.bufferedTheme Is Nothing) Then
                    DirectCast(Me.bufferedTheme, IDisposable).Dispose
                    Me.bufferedTheme = Nothing
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private Sub InitializeComponent()
            Dim manager As New ComponentResourceManager(GetType(ThemeConfigurationDialog))
            Me.designerTreeView = New TreeView
            Me.themeNameLabel = New Label
            Me.themeLocationLabel = New Label
            Me.themeNameTextBox = New TextBox
            Me.nameLocationTableLayoutPanel = New TableLayoutPanel
            Me.themeLocationButton = New Button
            Me.themeLocationTextBox = New TextBox
            Me.button3 = New Button
            Me.okButton = New Button
            Me.cancelButton = New Button
            Me.themePanel = New Panel
            Me.themeConfigPanel = New Panel
            Me.propertiesGrid = New PropertyGrid
            Me.previewLabel = New Label
            Me.selectDesignerLabel = New Label
            Me.dummyPreviewPanel = New Panel
            Me.previewButton = New Button
            Me.okCancelTableLayoutPanel = New TableLayoutPanel
            Me.nameLocationTableLayoutPanel.SuspendLayout
            Me.themePanel.SuspendLayout
            Me.themeConfigPanel.SuspendLayout
            Me.okCancelTableLayoutPanel.SuspendLayout
            MyBase.SuspendLayout
            manager.ApplyResources(Me.designerTreeView, "designerTreeView")
            Me.designerTreeView.Name = "designerTreeView"
            manager.ApplyResources(Me.themeNameLabel, "themeNameLabel")
            Me.themeNameLabel.Margin = New Padding(0, 0, 3, 3)
            Me.themeNameLabel.Name = "themeNameLabel"
            manager.ApplyResources(Me.themeLocationLabel, "themeLocationLabel")
            Me.themeLocationLabel.Margin = New Padding(0, 3, 3, 0)
            Me.themeLocationLabel.Name = "themeLocationLabel"
            manager.ApplyResources(Me.themeNameTextBox, "themeNameTextBox")
            Me.nameLocationTableLayoutPanel.SetColumnSpan(Me.themeNameTextBox, 2)
            Me.themeNameTextBox.Margin = New Padding(3, 0, 0, 3)
            Me.themeNameTextBox.Name = "themeNameTextBox"
            manager.ApplyResources(Me.nameLocationTableLayoutPanel, "nameLocationTableLayoutPanel")
            Me.nameLocationTableLayoutPanel.ColumnStyles.Add(New ColumnStyle)
            Me.nameLocationTableLayoutPanel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100!))
            Me.nameLocationTableLayoutPanel.ColumnStyles.Add(New ColumnStyle)
            Me.nameLocationTableLayoutPanel.Controls.Add(Me.themeNameLabel, 0, 0)
            Me.nameLocationTableLayoutPanel.Controls.Add(Me.themeNameTextBox, 1, 0)
            Me.nameLocationTableLayoutPanel.Controls.Add(Me.themeLocationButton, 2, 1)
            Me.nameLocationTableLayoutPanel.Controls.Add(Me.themeLocationLabel, 0, 1)
            Me.nameLocationTableLayoutPanel.Controls.Add(Me.themeLocationTextBox, 1, 1)
            Me.nameLocationTableLayoutPanel.Name = "nameLocationTableLayoutPanel"
            Me.nameLocationTableLayoutPanel.RowStyles.Add(New RowStyle(SizeType.Percent, 50!))
            Me.nameLocationTableLayoutPanel.RowStyles.Add(New RowStyle(SizeType.Percent, 50!))
            manager.ApplyResources(Me.themeLocationButton, "themeLocationButton")
            Me.themeLocationButton.Margin = New Padding(3, 3, 0, 0)
            Me.themeLocationButton.Name = "themeLocationButton"
            manager.ApplyResources(Me.themeLocationTextBox, "themeLocationTextBox")
            Me.themeLocationTextBox.Margin = New Padding(3, 3, 3, 0)
            Me.themeLocationTextBox.Name = "themeLocationTextBox"
            manager.ApplyResources(Me.button3, "button3")
            Me.button3.Name = "button3"
            manager.ApplyResources(Me.okButton, "okButton")
            Me.okButton.DialogResult = DialogResult.OK
            Me.okButton.Margin = New Padding(0, 0, 3, 0)
            Me.okButton.Name = "okButton"
            manager.ApplyResources(Me.cancelButton, "cancelButton")
            Me.cancelButton.DialogResult = DialogResult.Cancel
            Me.cancelButton.Margin = New Padding(3, 0, 3, 0)
            Me.cancelButton.Name = "cancelButton"
            Me.themePanel.Controls.Add(Me.themeConfigPanel)
            Me.themePanel.Controls.Add(Me.previewLabel)
            Me.themePanel.Controls.Add(Me.selectDesignerLabel)
            Me.themePanel.Controls.Add(Me.dummyPreviewPanel)
            manager.ApplyResources(Me.themePanel, "themePanel")
            Me.themePanel.Margin = New Padding(4)
            Me.themePanel.Name = "themePanel"
            Me.themeConfigPanel.Controls.Add(Me.designerTreeView)
            Me.themeConfigPanel.Controls.Add(Me.propertiesGrid)
            manager.ApplyResources(Me.themeConfigPanel, "themeConfigPanel")
            Me.themeConfigPanel.Name = "themeConfigPanel"
            Me.propertiesGrid.CommandsVisibleIfAvailable = True
            manager.ApplyResources(Me.propertiesGrid, "propertiesGrid")
            Me.propertiesGrid.Name = "propertiesGrid"
            Me.propertiesGrid.ToolbarVisible = False
            manager.ApplyResources(Me.previewLabel, "previewLabel")
            Me.previewLabel.Name = "previewLabel"
            manager.ApplyResources(Me.selectDesignerLabel, "selectDesignerLabel")
            Me.selectDesignerLabel.Name = "selectDesignerLabel"
            manager.ApplyResources(Me.dummyPreviewPanel, "dummyPreviewPanel")
            Me.dummyPreviewPanel.Name = "dummyPreviewPanel"
            manager.ApplyResources(Me.previewButton, "previewButton")
            Me.previewButton.Margin = New Padding(3, 0, 0, 0)
            Me.previewButton.Name = "previewButton"
            manager.ApplyResources(Me.okCancelTableLayoutPanel, "okCancelTableLayoutPanel")
            Me.okCancelTableLayoutPanel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 33!))
            Me.okCancelTableLayoutPanel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 33!))
            Me.okCancelTableLayoutPanel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 33!))
            Me.okCancelTableLayoutPanel.Controls.Add(Me.okButton, 0, 0)
            Me.okCancelTableLayoutPanel.Controls.Add(Me.cancelButton, 1, 0)
            Me.okCancelTableLayoutPanel.Controls.Add(Me.previewButton, 2, 0)
            Me.okCancelTableLayoutPanel.Name = "okCancelTableLayoutPanel"
            Me.okCancelTableLayoutPanel.RowStyles.Add(New RowStyle(SizeType.Percent, 100!))
            MyBase.AcceptButton = Me.okButton
            MyBase.CancelButton = Me.cancelButton
            manager.ApplyResources(Me, "$this")
            MyBase.Controls.Add(Me.nameLocationTableLayoutPanel)
            MyBase.Controls.Add(Me.okCancelTableLayoutPanel)
            MyBase.Controls.Add(Me.themePanel)
            MyBase.Controls.Add(Me.button3)
            MyBase.FormBorderStyle = FormBorderStyle.FixedDialog
            MyBase.MaximizeBox = False
            MyBase.MinimizeBox = False
            MyBase.Name = "ThemeConfigurationDialog"
            MyBase.ShowInTaskbar = False
            MyBase.HelpButton = True
            MyBase.SizeGripStyle = SizeGripStyle.Hide
            Me.nameLocationTableLayoutPanel.ResumeLayout(False)
            Me.nameLocationTableLayoutPanel.PerformLayout
            Me.themePanel.ResumeLayout(False)
            Me.themeConfigPanel.ResumeLayout(False)
            Me.okCancelTableLayoutPanel.ResumeLayout(False)
            Me.okCancelTableLayoutPanel.PerformLayout
            MyBase.ResumeLayout(False)
            MyBase.PerformLayout
        End Sub

        Private Sub InitializeControls()
            AddHandler MyBase.HelpButtonClicked, New CancelEventHandler(AddressOf Me.OnHelpClicked)
            Me.themeNameTextBox.Text = Me.bufferedTheme.Name
            Me.themeLocationTextBox.Text = Me.bufferedTheme.FilePath
            Me.propertiesGrid.PropertySort = PropertySort.Categorized
            Me.designerPreview = New DesignerPreview(Me)
            Me.dummyPreviewPanel.Parent.Controls.Add(Me.designerPreview)
            Me.designerPreview.TabStop = False
            Me.designerPreview.Location = Me.dummyPreviewPanel.Location
            Me.designerPreview.Size = Me.dummyPreviewPanel.Size
            Me.dummyPreviewPanel.Visible = False
            Me.designerPreview.Parent.Controls.Remove(Me.dummyPreviewPanel)
            Me.designerTreeView.ShowLines = False
            Me.designerTreeView.ShowPlusMinus = False
            Me.designerTreeView.ShowRootLines = False
            Me.designerTreeView.ShowNodeToolTips = True
            Me.designerTreeView.HideSelection = False
            Me.designerTreeView.ItemHeight = Math.Max(Me.designerTreeView.ItemHeight, &H12)
            ThemeConfigHelpers.PopulateActivities(Me.serviceProvider, Me.designerTreeView)
            Me.themeConfigPanel.Controls.Remove(Me.designerTreeView)
            Me.themeConfigPanel.Controls.Remove(Me.propertiesGrid)
            Me.designerTreeView.Dock = DockStyle.Left
            Me.splitter = New Splitter
            Me.splitter.Dock = DockStyle.Left
            Me.propertiesGrid.Dock = DockStyle.Fill
            Me.themeConfigPanel.Controls.AddRange(New Control() { Me.propertiesGrid, Me.splitter, Me.designerTreeView })
            AddHandler Me.themePanel.Paint, New PaintEventHandler(AddressOf Me.OnThemePanelPaint)
            AddHandler Me.previewButton.Click, New EventHandler(AddressOf Me.OnPreviewClicked)
            AddHandler Me.designerTreeView.AfterSelect, New TreeViewEventHandler(AddressOf Me.OnDesignerSelectionChanged)
            AddHandler Me.themeLocationButton.Click, New EventHandler(AddressOf Me.OnThemeLocationClicked)
            AddHandler Me.okButton.Click, New EventHandler(AddressOf Me.OnOk)
            AddHandler Me.propertiesGrid.PropertyValueChanged, New PropertyValueChangedEventHandler(AddressOf Me.OnThemePropertyChanged)
            AddHandler Me.themeNameTextBox.TextChanged, New EventHandler(AddressOf Me.OnThemeChanged)
            AddHandler Me.themeLocationTextBox.TextChanged, New EventHandler(AddressOf Me.OnThemeChanged)
            Me.designerTreeView.SelectedNode = If((Me.designerTreeView.Nodes.Count > 0), Me.designerTreeView.Nodes.Item(0), Nothing)
            Me.designerTreeView.SelectedNode.EnsureVisible
            Me.ShowPreview = True
        End Sub

        Protected Overrides Sub OnClosing(ByVal e As CancelEventArgs)
            MyBase.OnClosing(e)
            Me.bufferedTheme.ReadOnly = True
        End Sub

        Private Sub OnDesignerSelectionChanged(ByVal sender As Object, ByVal eventArgs As TreeViewEventArgs)
            Dim activityType As Type = If(((Not eventArgs.Node Is Nothing) AndAlso GetType(Activity).IsAssignableFrom([TryCast](eventArgs.Node.Tag,Type))), [TryCast](eventArgs.Node.Tag,Type), Nothing)
            Dim designer As IDesigner = Me.designerPreview.UpdatePreview(activityType)
            Dim objArray As Object() = Nothing
            If (activityType Is Nothing) Then
                If (Not eventArgs.Node Is Nothing) Then
                    objArray = If((eventArgs.Node.Parent Is Nothing), New Object() { Me.bufferedTheme.AmbientTheme }, DirectCast(ThemeConfigHelpers.GetDesignerThemes(Me.serviceProvider, Me.bufferedTheme, eventArgs.Node), Object()))
                End If
            Else
                objArray = If((Not designer Is Nothing), New Object() { Me.bufferedTheme.GetDesignerTheme([TryCast](designer,ActivityDesigner)) }, Nothing)
            End If
            Me.propertiesGrid.SelectedObjects = objArray
        End Sub

        Private Sub OnHelpClicked(ByVal sender As Object, ByVal e As CancelEventArgs)
            e.Cancel = True
            Me.ShowHelp
        End Sub

        Protected Overrides Sub OnHelpRequested(ByVal e As HelpEventArgs)
            Me.ShowHelp
            e.Handled = True
        End Sub

        Protected Overrides Sub OnLoad(ByVal e As EventArgs)
            MyBase.OnLoad(e)
            Try 
                Cursor.Current = Cursors.WaitCursor
                Me.InitializeControls
            Finally
                Cursor.Current = Cursors.Default
            End Try
        End Sub

        Private Sub OnOk(ByVal sender As Object, ByVal e As EventArgs)
            Dim error As String = String.Empty
            Dim control As Control = Nothing
            If Not Me.ValidateControls([error], control) Then
                MyBase.DialogResult = DialogResult.None
                DesignerHelpers.ShowError(Me.serviceProvider, [error])
                If (Not control Is Nothing) Then
                    Dim box As TextBox = [TryCast](control,TextBox)
                    If (Not box Is Nothing) Then
                        box.SelectionStart = 0
                        box.SelectionLength = If((Not box.Text Is Nothing), box.Text.Length, 0)
                    End If
                    control.Focus
                End If
            ElseIf (Not Me.bufferedTheme.FilePath.Equals(Me.themeLocationTextBox.Text.Trim, StringComparison.OrdinalIgnoreCase) AndAlso (DialogResult.No = DesignerHelpers.ShowMessage(Me.serviceProvider, DR.GetString("UpdateRelativePaths", New Object(0  - 1) {}), DR.GetString("WorkflowDesignerTitle", New Object(0  - 1) {}), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))) Then
                MyBase.DialogResult = DialogResult.None
            ElseIf Me.themeDirty Then
                Try 
                    Cursor.Current = Cursors.WaitCursor
                    ThemeConfigHelpers.EnsureDesignerThemes(Me.serviceProvider, Me.bufferedTheme, ThemeConfigHelpers.GetAllTreeNodes(Me.designerTreeView))
                    Me.bufferedTheme.ReadOnly = False
                    Me.bufferedTheme.Name = Me.themeNameTextBox.Text.Trim
                    Me.bufferedTheme.Description = DR.GetString("ThemeDescription", New Object(0  - 1) {})
                    Me.bufferedTheme.Save(Me.themeLocationTextBox.Text.Trim)
                    Me.themeDirty = False
                    Me.bufferedTheme.ReadOnly = True
                Catch obj1 As Object
                    DesignerHelpers.ShowError(Me.serviceProvider, DR.GetString("ThemeFileCreationError", New Object(0  - 1) {}))
                    Me.themeLocationTextBox.SelectionStart = 0
                    Me.themeLocationTextBox.SelectionLength = If((Not Me.themeLocationTextBox.Text Is Nothing), Me.themeLocationTextBox.Text.Length, 0)
                    Me.themeLocationTextBox.Focus
                    MyBase.DialogResult = DialogResult.None
                Finally
                    Cursor.Current = Cursors.Default
                End Try
            End If
        End Sub

        Private Sub OnOperatingSystemSettingsChanged(ByVal sender As Object, ByVal e As UserPreferenceChangedEventArgs)
            If ((e.Category = UserPreferenceCategory.Color) OrElse (e.Category = UserPreferenceCategory.VisualStyle)) Then
                Me.Font = Me.StandardFont
            End If
        End Sub

        Private Sub OnPreviewClicked(ByVal sender As Object, ByVal e As EventArgs)
            Me.ShowPreview = Not Me.ShowPreview
        End Sub

        Private Sub OnThemeChanged(ByVal sender As Object, ByVal e As EventArgs)
            Me.themeDirty = True
        End Sub

        Private Sub OnThemeLocationClicked(ByVal sender As Object, ByVal e As EventArgs)
            Dim dialog As New SaveFileDialog { _
                .AddExtension = True, _
                .DefaultExt = "*.wtm", _
                .Filter = DR.GetString("ThemeFileFilter", New Object(0  - 1) {}), _
                .RestoreDirectory = False _
            }
            If (dialog.ShowDialog(Me) = DialogResult.OK) Then
                Me.themeLocationTextBox.Text = dialog.FileName
            End If
        End Sub

        Private Sub OnThemePanelPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
            e.Graphics.DrawRectangle(SystemPens.ControlDark, 0, 0, (Me.themePanel.ClientSize.Width - 1), (Me.themePanel.ClientSize.Height - 2))
            If Me.previewShown Then
                Dim point As New Point((Me.propertiesGrid.Right + ((Me.dummyPreviewPanel.Left - Me.propertiesGrid.Right) / 2)), Me.themePanel.Margin.Top)
                Dim point2 As New Point(point.X, (Me.themePanel.Height - Me.themePanel.Margin.Bottom))
                e.Graphics.DrawLine(SystemPens.ControlDark, point, point2)
            End If
            Dim size As New Size(8, 8)
            [Using] pen As Pen = New Pen(Color.Black, 1!)
                pen.DashStyle = DashStyle.Dot
                e.Graphics.DrawLine(pen, CInt((Me.designerPreview.Left - size.Width)), CInt((Me.designerPreview.Top - 1)), CInt((Me.designerPreview.Right + size.Width)), CInt((Me.designerPreview.Top - 1)))
                e.Graphics.DrawLine(pen, CInt((Me.designerPreview.Left - size.Width)), CInt((Me.designerPreview.Bottom + 1)), CInt((Me.designerPreview.Right + size.Width)), CInt((Me.designerPreview.Bottom + 1)))
                e.Graphics.DrawLine(pen, CInt((Me.designerPreview.Left - 1)), CInt((Me.designerPreview.Top - size.Height)), CInt((Me.designerPreview.Left - 1)), CInt((Me.designerPreview.Bottom + size.Height)))
                e.Graphics.DrawLine(pen, CInt((Me.designerPreview.Right + 1)), CInt((Me.designerPreview.Top - size.Height)), CInt((Me.designerPreview.Right + 1)), CInt((Me.designerPreview.Bottom + size.Height)))
            End [Using]
        End Sub

        Private Sub OnThemePropertyChanged(ByVal sender As Object, ByVal e As PropertyValueChangedEventArgs)
            Me.themeDirty = True
        End Sub

        Private Sub ShowHelp()
            DesignerHelpers.ShowHelpFromKeyword(Me.serviceProvider, (GetType(ThemeConfigurationDialog).FullName & ".UI"))
        End Sub

        Private Function ValidateControls(<Out> ByRef [error] As String, <Out> ByRef control As Control) As Boolean
            [error] = String.Empty
            control = Nothing
            If ((Me.themeNameTextBox.Text Is Nothing) OrElse (Me.themeNameTextBox.Text.Trim.Length = 0)) Then
                [error] = DR.GetString("ThemeNameNotValid", New Object(0  - 1) {})
                control = Me.themeNameTextBox
                Return False
            End If
            If (Me.themeLocationTextBox.Text Is Nothing) Then
                [error] = DR.GetString("ThemePathNotValid", New Object(0  - 1) {})
                control = Me.themeNameTextBox
                Return False
            End If
            Dim path As String = Me.themeLocationTextBox.Text.Trim
            If (((path.IndexOfAny(Path.GetInvalidPathChars) >= 0) OrElse Not Path.IsPathRooted(path)) OrElse Not Path.HasExtension(path)) Then
                [error] = DR.GetString("ThemePathNotValid", New Object(0  - 1) {})
                control = Me.themeLocationTextBox
                Return False
            End If
            Dim fileNameWithoutExtension As String = Path.GetFileNameWithoutExtension(path)
            Dim extension As String = Path.GetExtension(path)
            If (((fileNameWithoutExtension Is Nothing) OrElse (fileNameWithoutExtension.Trim.Length = 0)) OrElse ((extension Is Nothing) OrElse (extension.Trim.Length = 0))) Then
                [error] = DR.GetString("ThemePathNotValid", New Object(0  - 1) {})
                control = Me.themeLocationTextBox
                Return False
            End If
            If Not extension.Equals("*.wtm".Replace("*", ""), StringComparison.Ordinal) Then
                [error] = DR.GetString("ThemeFileNotXml", New Object(0  - 1) {})
                control = Me.themeLocationTextBox
                Return False
            End If
            Return True
        End Function


        ' Properties
        Public ReadOnly Property ComposedTheme As WorkflowTheme
            Get
                Return Me.bufferedTheme
            End Get
        End Property

        Private Property ShowPreview As Boolean
            Get
                Return Me.previewShown
            End Get
            Set(ByVal value As Boolean)
                Me.previewShown = value
                Me.previewLabel.Visible = Me.previewShown
                Me.designerPreview.Visible = Me.previewShown
                If Me.previewShown Then
                    Me.themePanel.Width = (Me.designerPreview.Right + ((Me.designerPreview.Left - Me.propertiesGrid.Right) / 2))
                    Me.previewButton.Text = (DR.GetString("Preview", New Object(0  - 1) {}) & " <<")
                Else
                    Me.themePanel.Width = (Me.themeConfigPanel.Right + Me.themeConfigPanel.Left)
                    Me.previewButton.Text = (DR.GetString("Preview", New Object(0  - 1) {}) & " >>")
                End If
                MyBase.Width = (((Me.themePanel.Right + Me.themePanel.Left) + MyBase.Margin.Left) + MyBase.Margin.Right)
                Me.themePanel.Invalidate
            End Set
        End Property

        Private ReadOnly Property StandardFont As Font
            Get
                Dim menuFont As Font = SystemInformation.MenuFont
                If (Not Me.serviceProvider Is Nothing) Then
                    Dim service As IUIService = DirectCast(Me.serviceProvider.GetService(GetType(IUIService)), IUIService)
                    If (Not service Is Nothing) Then
                        menuFont = DirectCast(service.Styles.Item("DialogFont"), Font)
                    End If
                End If
                Return menuFont
            End Get
        End Property


        ' Fields
        Private bufferedTheme As WorkflowTheme
        Private button3 As Button
        Private cancelButton As Button
        Private components As IContainer
        Private designerPreview As DesignerPreview
        Private designerTreeView As TreeView
        Private dummyPreviewPanel As Panel
        Private nameLocationTableLayoutPanel As TableLayoutPanel
        Private okButton As Button
        Private okCancelTableLayoutPanel As TableLayoutPanel
        Private previewButton As Button
        Private previewLabel As Label
        Private previewShown As Boolean
        Private propertiesGrid As PropertyGrid
        Private selectDesignerLabel As Label
        Private serviceProvider As IServiceProvider
        Private splitter As Splitter
        Private themeConfigPanel As Panel
        Private themeDirty As Boolean
        Private themeLocationButton As Button
        Private themeLocationLabel As Label
        Private themeLocationTextBox As TextBox
        Private themeNameLabel As Label
        Private themeNameTextBox As TextBox
        Private themePanel As Panel

        ' Nested Types
        Friend NotInheritable Class DesignerPreview
            Inherits UserControl
            ' Methods
            Friend Sub New(ByVal parent As ThemeConfigurationDialog)
                Me.BackColor = Color.White
                Me.parent = parent
            End Sub

            Private Sub AddDummyActivity(ByVal parentActivity As CompositeActivity, ByVal activityType As Type)
                If TypeOf Me.surface.GetService(GetType(IDesignerHost)) Is IDesignerHost Then
                    Dim item As Activity = [TryCast](Activator.CreateInstance(activityType),Activity)
                    If (Not item Is Nothing) Then
                        parentActivity.Activities.Add(item)
                        Me.EnsureUniqueId(item)
                        WorkflowDesignerLoader.AddActivityToDesigner(Me.surface, item)
                    End If
                End If
            End Sub

            Protected Overrides Sub Dispose(ByVal disposing As Boolean)
                If (disposing AndAlso (Not Me.surface Is Nothing)) Then
                    Dim service As IDesignerHost = [TryCast](Me.GetService(GetType(IDesignerHost)),IDesignerHost)
                    If ((Not service Is Nothing) AndAlso (Not service.RootComponent Is Nothing)) Then
                        WorkflowDesignerLoader.RemoveActivityFromDesigner(Me.surface, [TryCast](service.RootComponent,Activity))
                    End If
                    Dim workflow As ReadonlyWorkflow = If((MyBase.Controls.Count > 0), [TryCast](MyBase.Controls.Item(0),ReadonlyWorkflow), Nothing)
                    MyBase.Controls.Clear
                    If (Not workflow Is Nothing) Then
                        workflow.Dispose
                        workflow = Nothing
                    End If
                    Me.surface.Dispose
                    Me.surface = Nothing
                End If
                MyBase.Dispose(disposing)
            End Sub

            Private Sub EnsureUniqueId(ByVal addedActivity As Activity)
                Dim dictionary As New Dictionary([Of] String, Integer)
                Dim queue As New Queue([Of] Activity)
                queue.Enqueue(addedActivity)
                Do While (queue.Count > 0)
                    Dim activity As Activity = queue.Dequeue
                    Dim fullName As String = activity.GetType.FullName
                    Dim num As Integer = If(dictionary.ContainsKey(fullName), dictionary.Item(fullName), 1)
                    activity.Name = (activity.GetType.Name & num.ToString(CultureInfo.InvariantCulture))
                    num += 1
                    If dictionary.ContainsKey(fullName) Then
                        dictionary.Item(fullName) = num
                    Else
                        dictionary.Add(fullName, num)
                    End If
                    Dim activity2 As CompositeActivity = [TryCast](activity,CompositeActivity)
                    If (Not activity2 Is Nothing) Then
                        Dim activity3 As Activity
                        For Each activity3 In activity2.Activities
                            queue.Enqueue(activity3)
                        Next
                    End If
                Loop
            End Sub

            Protected Overrides Sub OnLoad(ByVal e As EventArgs)
                MyBase.OnLoad(e)
                MyBase.SuspendLayout
                Me.surface = New PreviewDesignSurface(Me.parent.serviceProvider)
                Dim loader As New PreviewWorkflowDesignerLoader
                Me.surface.BeginLoad(loader)
                Dim service As IDesignerHost = [TryCast](Me.surface.GetService(GetType(IDesignerHost)),IDesignerHost)
                Dim activity As Activity = [TryCast](service.CreateComponent(Type.GetType("System.Workflow.Activities.SequentialWorkflowActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")),Activity)
                activity.Name = "ThemeSequentialWorkflow"
                WorkflowDesignerLoader.AddActivityToDesigner(Me.surface, activity)
                Dim workflow As New ReadonlyWorkflow(Me.parent, Me.surface) { _
                    .TabStop = False, _
                    .Dock = DockStyle.Fill _
                }
                MyBase.Controls.Add(workflow)
                service.Activate
                MyBase.ResumeLayout(True)
            End Sub

            Friend Function UpdatePreview(ByVal activityType As Type) As IDesigner
                Dim flag As Boolean = False
                Dim host As IDesignerHost = [TryCast](Me.surface.GetService(GetType(IDesignerHost)),IDesignerHost)
                Dim rootComponent As CompositeActivity = [TryCast](host.RootComponent,CompositeActivity)
                If ((host Is Nothing) OrElse (rootComponent Is Nothing)) Then
                    Return Nothing
                End If
                Dim component As IComponent = Nothing
                Try 
                    Do While (rootComponent.Activities.Count > 0)
                        Dim item As Activity = rootComponent.Activities.Item(0)
                        rootComponent.Activities.Remove(item)
                        WorkflowDesignerLoader.RemoveActivityFromDesigner(Me.surface, item)
                    Loop
                    If ((activityType Is Nothing) OrElse activityType.FullName.Equals("System.Workflow.Activities.SequentialWorkflowActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", StringComparison.OrdinalIgnoreCase)) Then
                        Me.AddDummyActivity(rootComponent, Type.GetType("System.Workflow.Activities.CodeActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"))
                        flag = True
                    Else
                        Dim componentArray As IComponent() = Nothing
                        Dim customAttributes As Object() = activityType.GetCustomAttributes(GetType(ToolboxItemAttribute), False)
                        Dim attribute As ToolboxItemAttribute = If(((Not customAttributes Is Nothing) AndAlso (customAttributes.GetLength(0) > 0)), [TryCast](customAttributes(0),ToolboxItemAttribute), Nothing)
                        If (((Not attribute Is Nothing) AndAlso (Not attribute.ToolboxItemType Is Nothing)) AndAlso GetType(ActivityToolboxItem).IsAssignableFrom(attribute.ToolboxItemType)) Then
                            componentArray = [TryCast](Activator.CreateInstance(attribute.ToolboxItemType, New Object() { activityType }),ActivityToolboxItem).CreateComponents(host)
                        End If
                        If (componentArray Is Nothing) Then
                            componentArray = New IComponent() { [TryCast](Activator.CreateInstance(activityType),IComponent) }
                        End If
                        Dim activity3 As Activity = If(((Not componentArray Is Nothing) AndAlso (componentArray.Length > 0)), [TryCast](componentArray(0),Activity), Nothing)
                        If (Not activity3 Is Nothing) Then
                            rootComponent.Activities.Add(activity3)
                            Me.EnsureUniqueId(activity3)
                            WorkflowDesignerLoader.AddActivityToDesigner(Me.surface, activity3)
                            Dim designer As CompositeActivityDesigner = [TryCast](host.GetDesigner(rootComponent),CompositeActivityDesigner)
                            Dim containedDesigner As ActivityDesigner = [TryCast](host.GetDesigner(activity3),ActivityDesigner)
                            If ((Not designer Is Nothing) AndAlso (Not containedDesigner Is Nothing)) Then
                                designer.EnsureVisibleContainedDesigner(containedDesigner)
                            End If
                        End If
                    End If
                    Dim service As ISelectionService = [TryCast](host.GetService(GetType(ISelectionService)),ISelectionService)
                    If (Not service Is Nothing) Then
                        service.SetSelectedComponents(New IComponent() { rootComponent })
                    End If
                    Dim workflow As ReadonlyWorkflow = If((MyBase.Controls.Count > 0), [TryCast](MyBase.Controls.Item(0),ReadonlyWorkflow), Nothing)
                    If (Not workflow Is Nothing) Then
                        workflow.PerformLayout
                    End If
                    component = If(((rootComponent.Activities.Count > 0) AndAlso Not flag), rootComponent.Activities.Item(0), rootComponent)
                Catch obj1 As Object
                End Try
                If (component Is Nothing) Then
                    Return Nothing
                End If
                Return host.GetDesigner(component)
            End Function


            ' Fields
            Private parent As ThemeConfigurationDialog
            Private surface As PreviewDesignSurface

            ' Nested Types
            Private NotInheritable Class PreviewDesignSurface
                Inherits DesignSurface
                ' Methods
                Friend Sub New(ByVal parentProvider As IServiceProvider)
                    MyBase.New(New PreviewDesignerServiceProvider(parentProvider))
                    If Not TypeOf MyBase.GetService(GetType(ITypeProvider)) Is ITypeProvider Then
                        Dim serviceInstance As New TypeProvider(Me)
                        serviceInstance.AddAssemblyReference(GetType(String).Assembly.Location)
                        MyBase.ServiceContainer.AddService(GetType(ITypeProvider), serviceInstance, True)
                    End If
                End Sub

                Protected Overrides Function CreateDesigner(ByVal component As IComponent, ByVal rootDesigner As Boolean) As IDesigner
                    Dim designer As IDesigner = MyBase.CreateDesigner(component, rootDesigner)
                    Dim activity As Activity = [TryCast](component,Activity)
                    If (((designer Is Nothing) AndAlso Not rootDesigner) AndAlso (Not activity Is Nothing)) Then
                        designer = ActivityDesigner.CreateDesigner(activity.Site, activity)
                    End If
                    Return designer
                End Function


                ' Nested Types
                Private NotInheritable Class PreviewDesignerServiceProvider
                    Implements IServiceProvider
                    ' Methods
                    Friend Sub New(ByVal serviceProvider As IServiceProvider)
                        Me.serviceProvider = serviceProvider
                    End Sub

                    Private Function System.IServiceProvider.GetService(ByVal serviceType As Type) As Object Implements IServiceProvider.GetService
                        If (serviceType Is GetType(IPropertyValueUIService)) Then
                            Return Nothing
                        End If
                        Return Me.serviceProvider.GetService(serviceType)
                    End Function


                    ' Fields
                    Private serviceProvider As IServiceProvider
                End Class
            End Class

            Private Class PreviewWorkflowDesignerLoader
                Inherits WorkflowDesignerLoader
                ' Methods
                Public Overrides Function GetFileReader(ByVal filePath As String) As TextReader
                    Return Nothing
                End Function

                Public Overrides Function GetFileWriter(ByVal filePath As String) As TextWriter
                    Return Nothing
                End Function


                ' Properties
                Public Overrides ReadOnly Property FileName As String
                    Get
                        Return String.Empty
                    End Get
                End Property

            End Class

            Private Class ReadonlyWorkflow
                Inherits WorkflowView
                ' Methods
                Friend Sub New(ByVal themeConfigDialog As ThemeConfigurationDialog, ByVal serviceProvider As IServiceProvider)
                    MyBase.New(serviceProvider)
                    Me.themeConfigDialog = themeConfigDialog
                    AddHandler Me.themeConfigDialog.propertiesGrid.PropertyValueChanged, New PropertyValueChangedEventHandler(AddressOf Me.OnThemePropertyChanged)
                    MyBase.EnableFitToScreen = False
                    MyBase.AddDesignerMessageFilter(New ReadonlyMessageFilter)
                End Sub

                Protected Overrides Sub Dispose(ByVal disposing As Boolean)
                    MyBase.Dispose(disposing)
                    If ((Not Me.themeConfigDialog Is Nothing) AndAlso (Not Me.themeConfigDialog.propertiesGrid Is Nothing)) Then
                        RemoveHandler Me.themeConfigDialog.propertiesGrid.PropertyValueChanged, New PropertyValueChangedEventHandler(AddressOf Me.OnThemePropertyChanged)
                    End If
                End Sub

                Protected Overrides Sub OnLayout(ByVal levent As LayoutEventArgs)
                    If (Not Me.themeConfigDialog Is Nothing) Then
                        [Using] New BufferedTheme(Me.themeConfigDialog.bufferedTheme)
                            MyBase.OnLayout(levent)
                        End [Using]
                        Dim extent As Size = MyBase.ActiveLayout.Extent
                        Dim size As Size = MyBase.Size
                        Dim tf As New PointF((CSng(size.Width) / CSng(extent.Width)), (CSng(size.Height) / CSng(extent.Height)))
                        MyBase.Zoom = Convert.ToInt32(CSng((Math.Min(tf.X, tf.Y) * 100!)))
                    End If
                End Sub

                Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
                    If (Me.themeConfigDialog Is Nothing) Then
                        MyBase.OnPaint(e)
                    Else
                        [Using] New BufferedTheme(Me.themeConfigDialog.bufferedTheme)
                            MyBase.OnPaint(e)
                        End [Using]
                    End If
                End Sub

                Private Sub OnThemePropertyChanged(ByVal sender As Object, ByVal e As PropertyValueChangedEventArgs)
                    If (Not Me.themeConfigDialog Is Nothing) Then
                        [Using] New BufferedTheme(Me.themeConfigDialog.bufferedTheme)
                            MyBase.OnThemeChange(WorkflowTheme.CurrentTheme, EventArgs.Empty)
                        End [Using]
                    End If
                End Sub


                ' Fields
                Private themeConfigDialog As ThemeConfigurationDialog

                ' Nested Types
                Private NotInheritable Class BufferedTheme
                    Implements IDisposable
                    ' Methods
                    Friend Sub New(ByVal themeToApply As WorkflowTheme)
                        If ((Not themeToApply Is Nothing) AndAlso (Not WorkflowTheme.CurrentTheme Is themeToApply)) Then
                            WorkflowTheme.EnableChangeNotification = False
                            Me.oldTheme = WorkflowTheme.CurrentTheme
                            WorkflowTheme.CurrentTheme = themeToApply
                        End If
                    End Sub

                    Private Sub System.IDisposable.Dispose() Implements IDisposable.Dispose
                        If ((Not Me.oldTheme Is Nothing) AndAlso (Not WorkflowTheme.CurrentTheme Is Me.oldTheme)) Then
                            WorkflowTheme.CurrentTheme.ReadOnly = False
                            WorkflowTheme.CurrentTheme = Me.oldTheme
                            WorkflowTheme.EnableChangeNotification = True
                        End If
                    End Sub


                    ' Fields
                    Private oldTheme As WorkflowTheme
                End Class
            End Class
        End Class

        Private Class ThemeConfigHelpers
            ' Methods
            Friend Shared Sub EnsureDesignerThemes(ByVal serviceProvider As IServiceProvider, ByVal workflowTheme As WorkflowTheme, ByVal items As TreeNode())
                Dim node As TreeNode
                For Each node In items
                    Dim tag As Type = [TryCast](node.Tag,Type)
                    If (Not tag Is Nothing) Then
                        Dim designerBaseType As Type = If(tag.FullName.Equals("System.Workflow.Activities.SequentialWorkflowActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", StringComparison.OrdinalIgnoreCase), GetType(IRootDesigner), GetType(IDesigner))
                        Dim designerType As Type = ActivityDesigner.GetDesignerType(serviceProvider, tag, designerBaseType)
                        If (Not designerType Is Nothing) Then
                            workflowTheme.GetTheme(designerType)
                        End If
                    End If
                Next
            End Sub

            Friend Shared Function GetAllTreeNodes(ByVal treeView As TreeView) As TreeNode()
                Dim list As New List([Of] TreeNode)
                Dim queue As New Queue([Of] TreeNodeCollection)
                queue.Enqueue(treeView.Nodes)
                Do While (queue.Count > 0)
                    Dim node As TreeNode
                    For Each node In queue.Dequeue
                        list.Add(node)
                        If (node.Nodes.Count > 0) Then
                            queue.Enqueue(node.Nodes)
                        End If
                    Next
                Loop
                Return list.ToArray
            End Function

            Friend Shared Function GetCatagoryNodeForDesigner(ByVal designerType As Type, ByVal treeNodes As TreeNode()) As TreeNode
                If (designerType Is Nothing) Then
                    Throw New ArgumentNullException("designerType")
                End If
                If (treeNodes Is Nothing) Then
                    Throw New ArgumentNullException("treeNodes")
                End If
                If (treeNodes.Length = 0) Then
                    Throw New ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "treeNodes")
                End If
                Dim attribute As CategoryAttribute = Nothing
                Dim attribute2 As CategoryAttribute = Nothing
                Dim type As Type = designerType
                Do While ((Not type Is GetType(Object)) AndAlso (attribute Is Nothing))
                    Dim customAttributes As Object() = type.GetCustomAttributes(GetType(CategoryAttribute), False)
                    If ((Not customAttributes Is Nothing) AndAlso (customAttributes.GetLength(0) > 0)) Then
                        If (attribute2 Is Nothing) Then
                            attribute2 = [TryCast](customAttributes(0),CategoryAttribute)
                        Else
                            attribute = [TryCast](customAttributes(0),CategoryAttribute)
                        End If
                    End If
                    type = type.BaseType
                Loop
                If (attribute2 Is Nothing) Then
                    Return Nothing
                End If
                Dim node As TreeNode = Nothing
                Dim node2 As TreeNode = treeNodes(0)
                Dim node3 As TreeNode
                For Each node3 In treeNodes
                    If (((Not attribute Is Nothing) AndAlso (attribute.Category = node3.Text)) AndAlso ((node3.Tag Is Nothing) OrElse Not GetType(Activity).IsAssignableFrom(node3.Tag.GetType))) Then
                        node2 = node3
                    End If
                    If ((attribute2.Category = node3.Text) AndAlso ((node3.Tag Is Nothing) OrElse Not GetType(Activity).IsAssignableFrom(node3.Tag.GetType))) Then
                        node = node3
                        Exit For
                    End If
                Next
                If ((node Is Nothing) AndAlso (Not node2 Is Nothing)) Then
                    node = New TreeNode(attribute2.Category)
                    node2.Nodes.Add(node)
                End If
                Return node
            End Function

            Friend Shared Function GetDesignerThemes(ByVal serviceProvider As IServiceProvider, ByVal workflowTheme As WorkflowTheme, ByVal selectedNode As TreeNode) As DesignerTheme()
                Dim list As New ArrayList
                Dim queue As New Queue([Of] TreeNode)
                queue.Enqueue(selectedNode)
                Do While (queue.Count > 0)
                    Dim node As TreeNode = queue.Dequeue
                    Dim tag As Type = [TryCast](node.Tag,Type)
                    If (Not tag Is Nothing) Then
                        Dim designerBaseType As Type = If(tag.FullName.Equals("System.Workflow.Activities.SequentialWorkflowActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", StringComparison.OrdinalIgnoreCase), GetType(IRootDesigner), GetType(IDesigner))
                        Dim designerType As Type = ActivityDesigner.GetDesignerType(serviceProvider, tag, designerBaseType)
                        If (Not designerType Is Nothing) Then
                            Dim theme As DesignerTheme = workflowTheme.GetTheme(designerType)
                            If (Not theme Is Nothing) Then
                                list.Add(theme)
                            End If
                        End If
                    Else
                        Dim node2 As TreeNode
                        For Each node2 In node.Nodes
                            queue.Enqueue(node2)
                        Next
                        [Continue] Do
                    End If
                Loop
                Return DirectCast(list.ToArray(GetType(DesignerTheme)), DesignerTheme())
            End Function

            Friend Shared Sub PopulateActivities(ByVal serviceProvider As IServiceProvider, ByVal treeView As TreeView)
                Dim list As New List([Of] Type)
                Dim str As String
                For Each str In New List([Of] String) { "System.Workflow.Activities.SequentialWorkflowActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "System.Workflow.Activities.StateMachineWorkflowActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "System.Workflow.Activities.IfElseBranchActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", GetType(FaultHandlersActivity).AssemblyQualifiedName, "System.Workflow.Activities.EventHandlersActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", GetType(CompensationHandlerActivity).AssemblyQualifiedName, GetType(CancellationHandlerActivity).AssemblyQualifiedName }
                    Dim type As Type = Type.GetType(str, False)
                    If (Not type Is Nothing) Then
                        list.Add(type)
                    End If
                Next
                Dim list3 As IList([Of] Type) = New List([Of] Type)
                treeView.BeginUpdate
                treeView.Nodes.Clear
                Dim node As New TreeNode(DR.GetString("WorkflowDesc", New Object(0  - 1) {}))
                treeView.Nodes.Add(node)
                Dim service As IToolboxService = [TryCast](serviceProvider.GetService(GetType(IToolboxService)),IToolboxService)
                Dim creator As ITypeProviderCreator = [TryCast](serviceProvider.GetService(GetType(ITypeProviderCreator)),ITypeProviderCreator)
                If ((Not service Is Nothing) AndAlso (Not creator Is Nothing)) Then
                Label_0243:
                    Dim item As ToolboxItem
                    For Each item In service.GetToolboxItems
                        Dim flag As Boolean = TypeOf item Is ActivityToolboxItem
                        If Not flag Then
                            Dim attribute As ToolboxItemFilterAttribute
                            For Each attribute In item.Filter
                                If (attribute.FilterString.StartsWith("Microsoft.Workflow.VSDesigner", StringComparison.OrdinalIgnoreCase) OrElse attribute.FilterString.StartsWith("System.Workflow.ComponentModel", StringComparison.OrdinalIgnoreCase)) Then
                                    flag = True
                                    Exit For
                                End If
                            Next
                        End If
                        If flag Then
                            Dim type2 As Type = Nothing
                            Dim transientAssembly As Assembly = creator.GetTransientAssembly(item.AssemblyName)
                            If (Not transientAssembly Is Nothing) Then
                                type2 = transientAssembly.GetType(item.TypeName)
                            End If
                            If (type2 Is Nothing) Then
                                goto Label_0243
                            End If
                            Dim info As ConstructorInfo
                            For Each info In type2.GetConstructors
                                If (info.IsPublic AndAlso (info.GetParameters.GetLength(0) = 0)) Then
                                    list.Add(type2)
                                End If
                            Next
                        End If
                    Next
                End If
                Dim type3 As Type
                For Each type3 In list
                    Dim designerBaseType As Type = If(type3.FullName.Equals("System.Workflow.Activities.SequentialWorkflowActivity, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", StringComparison.OrdinalIgnoreCase), GetType(IRootDesigner), GetType(IDesigner))
                    Dim type5 As Type = ActivityDesigner.GetDesignerType(serviceProvider, type3, designerBaseType)
                    If ((Not type5 Is Nothing) AndAlso Not list3.Contains(type5)) Then
                        Dim customAttributes As Object() = type5.GetCustomAttributes(GetType(ActivityDesignerThemeAttribute), True)
                        If (Not If(((Not customAttributes Is Nothing) AndAlso (customAttributes.GetLength(0) > 0)), [TryCast](customAttributes(0),ActivityDesignerThemeAttribute), Nothing) Is Nothing) Then
                            Dim toolboxImage As Image = ActivityToolboxItem.GetToolboxImage(type3)
                            If (treeView.ImageList Is Nothing) Then
                                treeView.ImageList = New ImageList
                                treeView.ImageList.ColorDepth = ColorDepth.Depth32Bit
                                Dim image As Image = DR.GetImage("Activity")
                                treeView.ImageList.Images.Add(image, AmbientTheme.TransparentColor)
                            End If
                            Dim catagoryNodeForDesigner As TreeNode = ThemeConfigHelpers.GetCatagoryNodeForDesigner(type5, ThemeConfigHelpers.GetAllTreeNodes(treeView))
                            If (Not catagoryNodeForDesigner Is Nothing) Then
                                Dim imageIndex As Integer = If((Not toolboxImage Is Nothing), treeView.ImageList.Images.Add(toolboxImage, AmbientTheme.TransparentColor), 0)
                                Dim node3 As TreeNode = If((imageIndex >= 0), New TreeNode(ActivityToolboxItem.GetToolboxDisplayName(type3), imageIndex, imageIndex), New TreeNode(ActivityToolboxItem.GetToolboxDisplayName(type3)))
                                node3.Tag = type3
                                Dim index As Integer = (catagoryNodeForDesigner.Nodes.Count - 1)
                                Do While ((index >= 0) AndAlso TypeOf catagoryNodeForDesigner.Nodes.Item(index).Tag Is Type)
                                    index -= 1
                                Loop
                                catagoryNodeForDesigner.Nodes.Insert(index, node3)
                            End If
                        End If
                    End If
                Next
                treeView.TreeViewNodeSorter = New ThemeTreeNodeComparer
                treeView.Sort
                treeView.Nodes.Item(0).ExpandAll
                treeView.EndUpdate
            End Sub

        End Class

        Friend NotInheritable Class ThemeTreeNodeComparer
            Implements IComparer
            ' Methods
            Private Function System.Collections.IComparer.Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
                Dim node As TreeNode = [TryCast](x,TreeNode)
                Dim node2 As TreeNode = [TryCast](y,TreeNode)
                If (node.Nodes.Count > node2.Nodes.Count) Then
                    Return 1
                End If
                Return String.Compare(node.Text, node2.Text, StringComparison.CurrentCulture)
            End Function

        End Class
    End Class
End Namespace

